package net.sf.jimex;

import com.atlassian.jira.issue.IssueFieldConstants;
import net.sf.jimex.jira.JiraIssuesManager;
import net.sf.jimex.project.Project;
import net.sf.jimex.project.Task;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
import org.apache.commons.digester.xmlrules.JimexDigesterRuleParser;

import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

/**
 * andrew 24.01.2006 23:48:17
 */
public class ProjectManager {
    public static final String DIGESTER_FILE = "project-rules.xml";
    private JiraIssuesManager issuesManager;

    public ProjectManager(JiraIssuesManager issuesManager) {
        this.issuesManager = issuesManager;
    }

    public void migrate(File projectFile) throws Exception {
        Project project = loadProject(projectFile);
        project.prepare();
        List tasks = project.getAllTasks();
        for (int i = 0; i < tasks.size(); i++) {
            Task task = (Task) tasks.get(i);
            String name = task.getName();
            if ("".equals(name)) {
                continue;
            }
            String issueKey = issuesManager.createIssue(name,
                    task.getDescription(),
                    task.getAssignee(),
                    task.getEstimated(),
                    IssueFieldConstants.BUG_TYPE_ID);
            createChildTasks(task, issueKey);
        }
        try {
            issuesManager.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createChildTasks(Task parentTask, String issueKey) throws Exception {
        List subTasks = parentTask.getSubTasks();

        boolean isPlainTask = false;
        for (Iterator iterator = subTasks.iterator(); iterator.hasNext();) {
            Task task = (Task) iterator.next();
            isPlainTask |= task.hasSubTasks();
            if (isPlainTask)
                break;
        }

        for (Iterator taskIterator = subTasks.iterator(); taskIterator.hasNext();) {
            Task childTask = (Task) taskIterator.next();
            String name = childTask.getName();

            String childIssueKey = issuesManager.createChildIssue(issueKey, name, childTask.getDescription(), childTask.getAssignee(),
                    childTask.getEstimated(), isPlainTask);

            createChildTasks(childTask, childIssueKey);
        }
    }


    public Project loadProject(File projectFile) throws Exception {
        Digester digester = new Digester();
        URL rulesFileUrl = getClass().getClassLoader().getResource(ProjectManager.DIGESTER_FILE);
        RuleSet ruleSet = new FromXmlRuleSet(rulesFileUrl, new JimexDigesterRuleParser(), digester);
        digester.addRuleSet(ruleSet);

        Project project = (Project) digester.parse(projectFile);
        project.prepare();
        return project;
    }
}
